$(() => {
    $(".tabTop-nav-list").css("display", "none")
    // let offleft = $(".saveimg").offset()
    // console.log(offleft)
    $(".btn-left").click(function () {
        let offleft = $(".saveimg").offset()
        console.log(offleft)
        if (offleft.left >= 135) {
            $(".saveimg").offset({
                left: offleft.left - 80
            })
        } else {
            $(".saveimg").offset({
                left: offleft.left
            })
        }
    })

    $(".btn-right").click(function () {
        let offleft = $(".saveimg").offset()
        console.log(offleft)
        if (offleft.left <= 134) {
            $(".saveimg").offset({
                left: offleft.left + 80
            })
        } else {
            $(".saveimg").offset({
                left: offleft.left
            })
        }
    })

    $(".saveimg img").mouseenter(function () {
        $(this).addClass("active").siblings().removeClass("active")
        $(".minImg>img").attr("src", `${$(this).attr("src")}`)
        $(".maxImg>img").attr("src", `${$(this).attr("src")}`)
    })


    let url = location.href;
    let id = url.split("=")[1]
    console.log(url, id)
    new Promise(function (resolve, reject) {
        $.ajax({
            type: "get",
            url: "../server/getdetilid.php",
            data: {
                name: id
            },
            dataType: "json",
            success: (data) => {
                renderUl(data)
                resolve();
                // $(".minImg>img").attr("src", `${data[0].photo}`)
                // $(".maxImg>img").attr("src", `${data[0].photo}`)
            }
        });
    }).then(function () {
        $(".numaddsub input").val("1")
        $(".num-sub").click(function () {
            let inpval = $(".numaddsub input").val()
            inpval = inpval * 1 - 1
            $(".numaddsub input").val(inpval)
            if ($(".numaddsub input").val() * 1 <= 1) {
                $(".numaddsub input").val("0")
            }
        })
        $(".num-add").click(function () {
            let inpval = $(".numaddsub input").val()
            inpval = inpval * 1 + 1
            $(".numaddsub input").val(inpval)
        })
        shopcar()
    })

    function renderUl(data) {
        let html = data.map((ele) => {
            return `
            <div class="right-part1">
                <i></i>
                <span>${ele.proPlace}</span>
                <span>${ele.saelsinfo}</span>
            </div>
            <div class="right-part2">
                <span class="p-title">${ele.title}</span>
            </div>
            <div class="right-part3">
                <p>甄选法国明星原料Peptan胶原蛋白肽，搭配日本弹性胶原蛋白，科学配比，更有血橙果粉、针叶樱桃浓缩粉等多重原料，呵护肌肤，焕亮新颜，重回水分年轻肌。</p>
            </div>
            <div class="right-part4">
                <span>售价</span>
                <div class="price-part">
                    <div class="price-part1">
                        <span class="p-price">${ele.price}</span>
                        <span class = "p-del">${ele.discountstr}</span>
                        <span>${ele.saelsinfo1}</span>
                        <span>${ele.saelsinfo2}</span>
                        <span>参考价：${ele.marketprice}</span>
                    </div>
                    <div class="price-part2">
                        <i></i>
                        <span>${ele.forepromotion}</span>
                    </div>
                </div>
            </div>
            <div class="right-part5">
                <p>税费
                    <span>${ele.saelsinfo1}</span>
                </p>
                <p>运费
                    <span>自营订单满88元包邮，不足金额的订单收取10元运费。
                        88元以商品活动后的总金额为准，不含税费，使用虚拟资产(优惠券、红包、考拉豆等)不影响包邮的计算。
                        特殊商品、自营店铺及入驻商家商品单独计算运费。</span>
                </p>
                <p>服务
                    <span>本商品由 自营保税仓 发货</span>
                </p>
                <p>数量
                    <div class="numaddsub">
                        <span class="num-sub">-</span>
                        <input type="text">
                        <span class="num-add">+</span>
                    </div>
                </p>
                <p>说明
                    <span>会员96折</span>
                    <span>正品保障</span>
                    <span>支持七天无理由退货</span>
                </p>
            </div>
            <div class="right-part6">
                <a>立即购买</a>
                <a class="addcar">加入购物车</a>
            </div>
            `
        }).join("")
        $(".main-right").html(html)
    }

    function shopcar() {
        let price = $(".p-price").text().slice(1)
        let title = $(".p-title").text()
        let num = $(".numaddsub>input").val()
        let count = price*num
        $(".addcar").click(function () {
            $.ajax({
                url: "../server/addcar.php",
                data: {
                    type: "add",
                    id,
                    price,
                    title,
                    num,
                    count
                },
                dataType: "json",
                success: function (response) {
                    console.log(response)
                }
            });
        })

    }

})